package com.cskaoyan;

import com.cskaoyan.vo.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * 创建日期: 2021/11/29 10:14
 *
 * @author ciggar
 */
public class MybatisMain {

    public static void main(String[] args) throws IOException {

        // 获取SqlSessionFactory，需要先获取SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        // 获取主配置文件输入流
//        ClassLoader classLoader = MybatisMain.class.getClassLoader();
//        InputStream stream = classLoader.getResourceAsStream("mybatis-config.xml");

        InputStream stream = Resources.getResourceAsStream("mybatis-config.xml");

        // 获取SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = builder.build(stream);

        // 获取SqlSession SqlSession可以帮助我们去执行SQL语句
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 可以根据SQLsession去执行对应的SQL语句
        Account account = sqlSession.selectOne("cskaoyan.selectAccountById", 1);

        // 打印Account
        System.out.println(account);

        // 关闭SQLsession
        sqlSession.close();

    }
}
